home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Other Langs / Tickle-4.0 (tcl) / tcl / extend / tclsrc / buildidx.tcl < prev    next >
Encoding:
Text File  |  1993-10-26  |  4.6 KB  |  139 lines  |  [TEXT/MPS ]

  1. #
  2. # buildidx.tcl --
  3. #
  4. # Code to build Tcl package library. Defines the proc `buildpackageindex'.
  5. #------------------------------------------------------------------------------
  6. # Copyright 1992-1993 Karl Lehenbauer and Mark Diekhans.
  7. #
  8. # Permission to use, copy, modify, and distribute this software and its
  9. # documentation for any purpose and without fee is hereby granted, provided
  10. # that the above copyright notice appear in all copies.  Karl Lehenbauer and
  11. # Mark Diekhans make no representations about the suitability of this
  12. # software for any purpose.  It is provided "as is" without express or
  13. # implied warranty.
  14. #------------------------------------------------------------------------------
  15. # $Id: buildidx.tcl,v 2.5 1993/07/20 08:35:45 markd Exp $
  16. #------------------------------------------------------------------------------
  17. #
  18.  
  19. #------------------------------------------------------------------------------
  20. # The following code passes around a array containing information about a
  21. # package.  The following fields are defined
  22. #
  23. #   o name - The name of the package.
  24. #   o offset - The byte offset of the package in the file.
  25. #   o procs - The list of entry point procedures defined for the package.
  26. #------------------------------------------------------------------------------
  27.  
  28. #------------------------------------------------------------------------------
  29. # Write a line to the index file describing the package.
  30. #
  31. proc TCLSH:PutIdxEntry {outfp pkgInfo endOffset} {
  32.     puts $outfp [concat [keylget pkgInfo name] \
  33.                         [keylget pkgInfo offset] \
  34.                         [expr {$endOffset - [keylget pkgInfo offset] - 1}] \
  35.                         [keylget pkgInfo procs]]
  36. }
  37.  
  38. #------------------------------------------------------------------------------
  39. # Parse a package header found by a scan match.  Handle backslashed
  40. # continuation lines.
  41. #
  42. proc TCLSH:ParsePkgHeader matchInfoVar {
  43.     upvar $matchInfoVar matchInfo
  44.  
  45.     set line [string trimright $matchInfo(line)]
  46.     while {[string match {*\\} $line]} {
  47.         set line [csubstr $line 0 [expr [clength $line]-1]]
  48.         append line " " [string trimright [gets $matchInfo(handle)]]
  49.     }
  50.  
  51.     keylset pkgInfo name   [lindex $line 1]
  52.     keylset pkgInfo offset [tell $matchInfo(handle)]
  53.     keylset pkgInfo procs  [lrange $line  2 end]
  54.     return $pkgInfo
  55. }
  56.  
  57. #------------------------------------------------------------------------------
  58. # Do the actual work of creating a package library index from a library file.
  59. #
  60. proc TCLSH:CreateLibIndex {libName} {
  61.  
  62.     if {[file extension $libName] != ".tlib"} {
  63.         error "Package library `$libName' does not have the extension `.tlib'"
  64.     }
  65.     set idxName "[file root $libName].tndx"
  66.  
  67.     unlink -nocomplain $idxName
  68.     set libFH [open $libName r]
  69.     set idxFH [open $idxName w]
  70.  
  71.     set contectHdl [scancontext create]
  72.  
  73.     scanmatch $contectHdl "^#@package: " {
  74.         if {[llength $matchInfo(line)] < 2} {
  75.             error "invalid package header \"$matchInfo(line)\""
  76.         }
  77.         if ![lempty $pkgInfo] {
  78.             TCLSH:PutIdxEntry $idxFH $pkgInfo $matchInfo(offset)
  79.         }
  80.         set pkgInfo [TCLSH:ParsePkgHeader matchInfo]
  81.     }
  82.  
  83.     scanmatch $contectHdl "^#@packend" {
  84.         if [lempty $pkgInfo] {
  85.             error "#@packend without #@package in $libName"
  86.         }
  87.         TCLSH:PutIdxEntry $idxFH $pkgDefName $pkgDefWhere $matchInfo(offset) \
  88.                           $pkgDefProcs
  89.         set pkgInfo {}
  90.     }
  91.  
  92.     set pkgInfo {}
  93.     if {[catch {
  94.         scanfile $contectHdl $libFH
  95.         if [lempty $pkgInfo] {
  96.             error "No #@package definitions found in $libName"
  97.         }   
  98.     } msg] != 0} {
  99.        global errorInfo errorCode
  100.        close $libFH
  101.        close $idxFH
  102.        unlink -nocomplain $idxName
  103.        error $msg $errorInfo $errorCode
  104.     }
  105.     if ![lempty $pkgInfo] {
  106.         TCLSH:PutIdxEntry $idxFH $pkgInfo [tell $libFH] 
  107.     }
  108.     close $libFH
  109.     close $idxFH
  110.     
  111.     scancontext delete $contectHdl
  112.  
  113.     # Set mode and ownership of the index to be the same as the library.
  114.     # Ignore errors if you can't set the ownership.
  115.  
  116.     file stat $libName statInfo
  117.     chmod $statInfo(mode) $idxName
  118.     catch {
  119.        chown [list $statInfo(uid) $statInfo(gid)] $idxName
  120.     }
  121. }
  122.  
  123. #------------------------------------------------------------------------------
  124. # Create a package library index from a library file.
  125. #
  126. proc buildpackageindex {libfile} {
  127.  
  128.     set status [catch {
  129.         TCLSH:CreateLibIndex $libfile
  130.     } errmsg]
  131.     if {$status != 0} {
  132.         global errorInfo errorCode
  133.         error "building package index for `$libfile' failed: $errmsg" \
  134.               $errorInfo $errorCode
  135.     }
  136. }
  137.  
  138.